package com.example.shouybl.fragment;


import android.os.Bundle;
import android.support.v4.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.example.shouybl.BaseApp;
import com.example.shouybl.R;
import com.example.shouybl.adapter.LikeAdapter;
import com.example.shouybl.bean.AppBaseBean;
import com.example.shouybl.bean.MoneyGame;
import com.example.shouybl.bean.MoneyGameBase;
import com.example.shouybl.bean.MoneyGameLike;
import com.example.shouybl.constant.Path;

import com.example.shouybl.widget.GridViewForScrollView;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.RequestParams;
import com.loopj.android.http.TextHttpResponseHandler;


import java.util.ArrayList;

import cz.msebera.android.httpclient.Header;

/**
 * 赚钱详情，游戏信息
 * A simple {@link Fragment} subclass.
 */
public class GameNewsFragment extends Fragment {


    private android.widget.TextView tvgamedesc;
    private com.example.shouybl.widget.GridViewForScrollView gvgamenews;
    private String id = "";
    // private String str = "";
    private ArrayList<MoneyGameLike> gameLikes = new ArrayList<>();
    private LikeAdapter adapter;
    private RequestParams likePar;

    private MoneyGame game = new MoneyGame();


    /**
     * 接收传过来的值
     *
     * @param savedInstanceState
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        id = getArguments().getString("id");
        // str = getArguments().getString("content");
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_game_news, container, false);
        initView(view);
        initData();
        return view;
    }

    private void initData() {
        AsyncHttpClient client = new AsyncHttpClient();
        //游戏详情数据
        // tvgamedesc.setText(str);
        client.post(Path.PATH_MONEY_DETAIL, getMoneyParams(), new TextHttpResponseHandler() {
            @Override
            public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {

            }

            @Override
            public void onSuccess(int statusCode, Header[] headers, String responseString) {
                TypeReference<MoneyGameBase> type = new TypeReference<MoneyGameBase>() {
                };
                MoneyGameBase gameBase = JSONObject.parseObject(responseString, MoneyGameBase.class);
                game = gameBase.getInfo();
                tvgamedesc.setText(game.getGame_desc());
            }
        });

        //猜你喜欢推荐数据
        client.post(Path.PATH_MONEY_LIKE, getLikePar(), new TextHttpResponseHandler() {
            @Override
            public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {

            }

            @Override
            public void onSuccess(int statusCode, Header[] headers, String responseString) {
                TypeReference<AppBaseBean<MoneyGameLike>> typeReference = new TypeReference<AppBaseBean<MoneyGameLike>>() {
                };
                AppBaseBean baseBean = JSONObject.parseObject(responseString, typeReference);
                if (baseBean != null && baseBean.getState().equals("success")) {
                    gameLikes.addAll(baseBean.getInfo());
                }
                adapter.notifyDataSetChanged();
            }
        });
    }

    //详情
    private RequestParams getMoneyParams() {
        RequestParams params = new RequestParams();
        params.add("id", id);
        params.add("compare", "02746f27bbc4108a98605b192ea140ea");
        return params;
    }

    //喜欢
    public RequestParams getLikePar() {
        likePar = new RequestParams();
        likePar.put("id", id);
        likePar.put("platform", "2");
        likePar.put("compare", "2a51d0e57750bb470a13b4bd14e2be59");
        return likePar;
    }

    private void initView(View view) {
        this.gvgamenews = (GridViewForScrollView) view.findViewById(R.id.gv_game_news);
        this.tvgamedesc = (TextView) view.findViewById(R.id.tv_game_desc);
        adapter = new LikeAdapter(gameLikes, BaseApp.getInstance());
        gvgamenews.setAdapter(adapter);
    }
}
